 <!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', sans-serif;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: #f0f2f5;
        }

        .container {
            background: #fff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            width: 400px;
        }

        h2 {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            color: #666;
        }

        input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }

        input:focus {
            outline: none;
            border-color: #007bff;
            box-shadow: 0 0 5px rgba(0,123,255,0.3);
        }

        button {
            width: 100%;
            padding: 12px;
            background: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background 0.3s;
        }

        button:hover {
            background: #0056b3;
        }

        .toggle-form {
            text-align: center;
            margin-top: 20px;
        }

        .toggle-form a {
            color: #007bff;
            text-decoration: none;
            cursor: pointer;
        }

        .toggle-form a:hover {
            text-decoration: underline;
        }

        @media (max-width: 480px) {
            .container {
                width: 90%;
                padding: 20px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <form id="loginForm" action="/login/" method="post">
            {% csrf_token %}
            <h2>用户登录</h2>
            <div class="form-group">
                <label>用户名/邮箱</label>
                <input type="text" required autocomplete="username" name="username">
            </div>
            <div class="form-group">
                <label>密码</label>
                <input type="password" required autocomplete="current-password" name="password">
            </div>
            <button type="submit">登录</button>
            <div class="toggle-form">
                <a href="{% url 'app:register'%}">没有账号？ 立即注册</a>
            </div>
        </form>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
              if (username === '') {
               alert('用户名不能为空');
                    return;
                    }
              if (password === '') {
               alert('密码不能为空');
                    return;
                     }
    fetch('/login', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            username: username,
            password: password
        })
    })
   .then(response => response.json())
   .then(data => {
        if (data.success) {
            // 登录成功处理，比如跳转到用户主页
            window.location.href = '/user/home';
        } else {
            // 登录失败处理，提示错误信息
            alert(data.message);
        }
    })
   .catch(error => {
        console.error('登录请求出错:', error);
        alert('登录过程中出现错误，请稍后再试');
    });
});
      }
    </script>
</body>
</html>